package com.autoEcole.service;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.autoEcole.dao.EtatDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Etat;

public class EtatService {
	
	private EtatDAO etatDAO;
	/**
	 * EtatService
	 */
	private static EtatService etatService;
	
	/**
	 * Constructor
	 */
	private EtatService() {
		super();
	}

	/**
	 * Returns the EtatService instance (Design Pattern : Singleton)
	 * 
	 * @return EtatService
	 */
	public static synchronized EtatService getInstance() {
		if (etatService == null)
			etatService = new EtatService();
		return etatService;
	}

	public int create(Etat etat) throws ApplicationException {
		try {
			return etatDAO.create(etat);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	public EtatDAO getEtatDAO() {
		return etatDAO;
	}

	public void setEtatDAO(EtatDAO etatDAO) {
		this.etatDAO = etatDAO;
	}

	/**
	 * 
	 */
	public void update(Etat etat) throws ApplicationException {
		try {
			etatDAO.update(etat);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public Etat findById(int id) throws ApplicationException {
		try {
			return etatDAO.findById(id);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * @throws ParseException 
	 * 
	 */
	public List<Etat> findAll() throws ApplicationException, ParseException {
		try {
			return etatDAO.findAll();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
	
	/**
	 * 
	 */
	public Map<Integer , Etat> findAllMap() throws ApplicationException {
		try {
			return etatDAO.findAllMap();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(int etatId) throws ApplicationException {
		try {
			etatDAO.delete(etatId);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}	
}
